package com.example.demo.demos.web;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class test {

    public static void main(String[] args) {

        String shopifyUrl = "https://lamassubikes.myshopify.com/admin/api/2024-10/graphql.json";
        String accessToken = "{access_token}"; // 替换为你的Shopify访问令牌

        String jsonInputString = "{" +
                "\"query\": \"mutation StorefrontAccessTokenCreate($input: StorefrontAccessTokenInput!) { storefrontAccessTokenCreate(input: $input) { userErrors { field message } shop { id } storefrontAccessToken { accessScopes { handle } accessToken title } } } \"," +
                "\"variables\": {" +
                "    \"input\": {" +
                "      \"title\": \"New Storefront Access Token\"" +
                "    }" +
                "  }" +
                "}";

        try {
            URL url = new URL(shopifyUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("X-Shopify-Access-Token", accessToken);
            connection.setDoOutput(true);

            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            int responseCode = connection.getResponseCode();
            System.out.println("POST Response Code :: " + responseCode);

            // 这里可以添加代码来处理响应体

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
